
estimation_data <-
  estimation_data %>%
  mutate(separate_vars = case_when(campaign_prob_success == "High chance" & campaign_expend == "High expenditure" ~ "Expenditure: High & Predicted Success: High",
                                   campaign_prob_success == "High chance" & campaign_expend == "Low expenditure" ~ "Expenditure: Low & Predicted Success: High",
                                   campaign_prob_success == "Low chance" & campaign_expend == "High expenditure" ~ "Expenditure: High & Predicted Success: Low",
                                   campaign_prob_success == "Low chance" & campaign_expend == "Low expenditure" ~ "Expenditure: Low & Predicted Success: Low",
                                   TRUE ~ NA_character_))


mod1 <- feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: High & Predicted Success: High"), cluster = "ID")
mod2 <- feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: Low & Predicted Success: High"), cluster = "ID")
mod3 <- feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: High & Predicted Success: Low"), cluster = "ID")
mod4 <- feols(avg_score_trust_elect ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: Low & Predicted Success: Low"), cluster = "ID")

mod5 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: High & Predicted Success: High"), cluster = "ID")
mod6 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: Low & Predicted Success: High"), cluster = "ID")
mod7 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: High & Predicted Success: Low"), cluster = "ID")
mod8 <- feols(avg_score_democ_support ~ pre_post*validated_election_outcome, data = estimation_data %>% filter(separate_vars == "Expenditure: Low & Predicted Success: Low"), cluster = "ID")


mods <- list(mod1, mod2, mod3, mod4, mod5, mod6, mod7, mod8)

table_df <-
  modelsummary(mods,
               stars = c("*" = 0.1, "**" = 0.05, "***" = 0.01),
               coef_map = c(
                 "pre_post" = "Post Election",
                 "validated_election_outcomeNot Elected" = "Outcome: Lost",
                 "pre_post:validated_election_outcomeNot Elected" = "Post x Lost"
               ),
               gof_map = c("nobs", "r.squared"),
               output = "data.frame"
  ) %>%
  mutate(term = ifelse(statistic == "std.error", "", term)) %>%
  select(-c(part, statistic)) %>%
  mutate_all(as.character)


# Append the new row to the table
latex_table <-
  table_df %>%
  kableExtra::kable("latex",
                    booktabs = T,
                    longtable = T,
                    escape = F,
                    cap = "\\label{tab:money_expect}Main Analysis, by Expenditures and Expectations of Success",
                    padding = 0,
                    col.names = linebreak(c("", "(1)", "(2)", "(3)", "(4)", "(5)", "(6)", "(7)", "(8)"), c("l", rep("c", 4))),
                    align = c("l", rep("c", 8)),
                    linesep = "") %>%
  kableExtra::row_spec(6, hline_after = T) %>%
  kableExtra::kable_styling(
    font_size = 9,
    latex_options = c("scale_down",
                      "hold_position",
                      "striped")) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "High $" = 1,
      "Low $" = 1,
      "High $" = 1,
      "Low $" = 1,
      "High $" = 1,
      "Low $" = 1,
      "High $" = 1,
      "Low $" = 1
    ),
    align = c("l", rep("c", 8))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "High Chance" = 2,
      "Low Chance" = 2,
      "High Chance" = 2,
      "Low Chance" = 2
    ),
    align = c("l", rep("c", 8))
  ) %>%
  kableExtra::add_header_above(
    header = c(
      " " = 1,
      "Trust in Elec." = 4,
      "Faith in Dem." = 4
    ),
    align = c("l", rep("c", 8))
  )

cat("\\renewcommand{\\arraystretch}{0.9}\n", file = "./outputs/tables/table_a10.tex")
cat(latex_table, file = "./outputs/tables/table_a10.tex", append = TRUE)



